/**
 * Created by dale on 2017/10/19.
 */
var classList;
var startDom;
var endDom;
var iUserId;
$(document).ready(function () {
    classList = $('#classList');
    $('.xh-datagrid-toolbar').parseToolbar();
    iUserId     = addressUri.getParam('iUserId');
    startDom    = $('#List_Search_StartTime');
    endDom      = $('#List_Search_EndTime');
    startDom.datebox();
    endDom.datebox();

    startDom.datebox('setValue', GetDateStr(0));
    endDom.datebox('setValue', GetDateStr(+30));

    classList.initDataGrid(baseUrl + 'frontend/tch/getClass',{queryParams:getParams()}).setSelectThenEnable(['bt_list_del'],['bt_list_feedback']);

    $('#bt_list_add').click(function () {
        window.top.xhTop.showModal({
            title:'新建公开课',
            content:'<iframe src="'+baseUrl+'Tch/pClassForm?iUserId='+iUserId+'"  style="width: 100%;;border-style: none;padding: 0px;height:420px"  scrolling="yes"   ></iframe>',
            button:'提交',
            size:'lg',
            backClose:false
        });
    });

    $('#list_depart').combotree({
        url: '/Device/getDepData',
        animate:true,
        onLoadSuccess:function(node,data){
            $length = data.length;
            $.each(data,function (id,value) {
                $length = $length + value.children.length;
            })
        },
        onShowPanel:function(){
            if ($length <= 8){
                $(this).combobox('panel').height("auto");//高度自适应
            }else{
                $(this).combobox('panel').height("200");//高度自定义
            }
        },
        onBeforeSelect: function (node) {
            var t = $(this).tree;
            var isLeaf = t('isLeaf', node.target);
            if (!isLeaf) {//选择的不是叶子节点
                return false;
            }
        }
    });

    $('#bt_list_del').click(function () {
        if( xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids=classList.getDataGridSelectedFields('iPCId');
        myWindow.initPopUp({title: '确定要删除么?', text: '这些公开课将被删除!'},function () {
            $.ajax({
                url:baseUrl+'Tch/delClass',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    classList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'success'
                    );
                }
            });
        });
    });

    $('#bt_list_feedback').click(function () {
        if( xhExtra.buttonDisabled($(this))){
            return false;
        }
        var iPCId = classList.getDataGridSelectedField('iPCId');
        var userId = classList.getDataGridSelectedField('iUserId');
        window.top.xhTop.showModal({
            title:'反馈信息',
            content:'<iframe src="'+baseUrl+'Tch/feedback?iPCId='+iPCId+'&iUserId='+userId+'"  style="width: 100%;;border-style: none;padding: 0px;height:350px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose:false
        });
    });




    $(function () {

        classList.datagrid({
            view: detailview,
            columns:[[

                {field:'iPCId'        ,title:'iPCId'     ,align:"center"   ,halign:"center",width:'80px',hidden:true},
                {field:'backInfo'        ,title:'#1'     ,align:"center"   ,halign:"center",width:'30px'},
                {field:'fileInfo'        ,title:'#2'     ,align:"center"   ,halign:"center",width:'30px'},
                {field:'sUserName'    ,title:'教师名称'   ,align:"left"     ,halign:"center",width:'120px'},
                {field:'sSchoolName'  ,title:'学校名称'   ,align:"left"     ,halign:"center",width:"220px"},
                {field:'tStartTime'   ,title:'开始时间'   ,align:"center"   ,halign:"center",width:"120px"},
                {field:'tEndTime'     ,title:'结束时间'   ,align:"center"   ,halign:"center",width:"120px"},
                {field:'iPCType'      ,title:'公开课类型' ,align:"left"     ,halign:"center",width:"80px"},
                {field:'sPlace'       ,title:'公开课地点' ,align:"left"     ,halign:"center",width:"180px"},
                {field:'sAttendClass' ,title:'班级'      ,align:"left"     ,halign:"center",width:"120px"},
                {field:'iStuNum'      ,title:'学生人数'   ,align:"right"     ,halign:"center",width:"80px"},
                {field:'iAttendNum'   ,title:'听课人数'   ,align:"right"     ,halign:"center",width:"80px"},
                {field:'iRunChargeId'   ,title:'运营负责人'   ,align:"left"     ,halign:"center",width:"120px",hidden:true},
                {field:'iAtClassChargeId'   ,title:'听课负责人'   ,align:"left"     ,halign:"center",width:"120px",hidden:true},
                {field:'iWebChargeId'   ,title:'网络负责人'   ,align:"left"     ,halign:"center",width:"120px",hidden:true},
                {field:'iTechChargeId'   ,title:'技术负责人'   ,align:"left"     ,halign:"center",width:"120px",hidden:true},
                {field:'iMarkChargeId'   ,title:'市场负责人'   ,align:"left"     ,halign:"center",width:"120px",hidden:true},

                {field:'tCreateDate'   ,title:'创建时间'   ,align:"center"     ,halign:"center",width:"150px"},
                {field:'iCreateUserId'   ,title:'创建用户'   ,align:"left"     ,halign:"center",width:"100px"},
                {field:'tLastModifyDate'   ,title:'最后修改时间'   ,align:"center"     ,halign:"center",width:"150px"},
                {field:'iLastModifyUserId'   ,title:'最后修改用户'   ,align:"left"     ,halign:"center",width:"100px"},

            ]],
            detailFormatter:function(index,row){
                var str='<div class="ddv" style="padding:5px 0;overflow: auto"  ><table style="width: 100%" >';


                if(row['mAttendences']!=null){
                    str+='<tr><td style="width: 100px">人员情况:</td><td><p>'+row['mAttendences']+'</p></td></tr>';
                    str+='<tr><td colspan="2"><hr style="margin: 5px 0px"></td></tr>';
                }
                if(row['mDesc']!=null){
                    str+='<tr><td style="width: 100px">备注:</td><td><p>'+row['mDesc']+'</p></td></tr>';
                    str+='<tr><td colspan="2"><hr style="margin: 5px 0px"></td></tr>';
                }
                if(row['mContent']!=null){
                    str+='<tr><td style="width: 100px">主要内容:</td><td><p>'+row['mContent']+'</p></td></tr>';
                    str+='<tr><td colspan="2"><hr style="margin: 5px 0px"></td></tr>';
                }
                if(row['mEvaluate']!=null){
                    str+='<tr><td style="width: 100px">评价:</td><td><p>'+row['mEvaluate']+'</p></td></tr>';
                    str+='<tr><td style="width: 100px">评价人:</td><td><span style="margin-right: 10px">'+row['iEvalId']+'</span><span style="margin-right: 10px">'+row['tEvalTime']+'</span><span><b>联系电话:</b>'+row['sSelfPhone']+'</span></td></tr>';
                    str+='<tr><td colspan="2"><hr style="margin: 5px 0px"></td></tr>';
                }
                if(row['sImages']!=null){
                    str+='<tr><td colspan="2" style="padding-left: 100px">'+timeLine.showImage(row['sImages'],row['iPCId'],row['downUrl'],row['showDel'])+'</td></tr>';
                }
                if(row['sFiles']!=null){
                    str+='<tr><td colspan="2" style="padding-left: 100px">'+timeLine.showFile(row['sFiles'],row['iPCId'],row['downUrl'],row['showDel'])+'</td></tr>';
                }

                str+='</table></div>';
                return str;
            },
            onExpandRow: function(index,row){
                var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                ddv.panel({
                    // height:200,
                    width:780,
                    border:false,
                    cache:false,
                    onLoad:function(){
                        classList.datagrid('fixDetailRowHeight',index);
                        classList.datagrid('fixDetailRowWidth',index);
                    }
                });
                classList.datagrid('fixDetailRowHeight',index);
            },
            onHeaderContextMenu: function(e, field){
                e.preventDefault();
                if (!cmenu){
                    createColumnMenu();
                }
                cmenu.menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            }
        });

        var cmenu = null;
        function createColumnMenu() {
            cmenu = $('<div/>').appendTo('body');
            cmenu.menu({
                onClick: function (item) {
                    if (item.iconCls == 'fa fa-check') {
                        classList.datagrid('hideColumn', item.name);
                        cmenu.menu('setIcon', {
                            target: item.target,
                            iconCls: 'fa fa-close'
                        });
                    } else {
                        classList.datagrid('showColumn', item.name);
                        cmenu.menu('setIcon', {
                            target: item.target,
                            iconCls: 'fa fa-check'
                        });
                    }
                }
            });

            var fields = classList.datagrid('getColumnFields');
            for (var i = 1; i < fields.length; i++) {
                var field = fields[i];
                var col = classList.datagrid('getColumnOption', field);
                console.log(col);
                if(col.hidden){
                    cmenu.menu('appendItem', {
                        text: col.title,
                        name: field,
                        iconCls: 'fa fa-close'
                    });
                }else {
                    cmenu.menu('appendItem', {
                        text: col.title,
                        name: field,
                        iconCls: 'fa fa-check'
                    });
                }

            }
        };






        DataGrid.onLoadSuccess=function (data) {
            if(iUserId!=null){
                var row = classList.datagrid("getRows");
                for (var r = 0; r < row.length; r++)
                {
                    classList.datagrid("expandRow",r);
                }
                $('.images').viewer();
            }
            //删除图片ajax
            $('.ddv').on('click','[id^=imageDelete_]',function () {
                console.log(this);
                var iPCId=$(this).attr("id").split("_")[1];
                var rmId=$(this).attr('id').split('_')[2];
                myWindow.initPopUp({text: '你确定要删除这张图片吗?'},function () {
                    $.ajax({
                        url:baseUrl+'Tch/ajaxDelete2',
                        data:{'id':iPCId,'rmId':rmId},
                        dataType:'json',
                        type:'post',
                        success:function (data) {
                            //根据数据更新
                            classList.datagrid('reload');
                        },
                        error:function (result) {
                            console.log(result);
                        }
                    });
                });
            });
            //删除附件ajax
            $('.ddv').on('click','[id^=fileDelete_]',function () {
                console.log(this);
                var iPCId=$(this).attr("id").split("_")[1];
                var rmId=$(this).attr('id').split('_')[2];
                myWindow.initPopUp({text: '你确定要删除这张图片吗?'},function () {
                    $.ajax({
                        url:baseUrl+'Tch/ajaxDelete2/fileDelete',
                        data:{'id':iPCId,'rmId':rmId},
                        dataType:'json',
                        type:'post',
                        success:function (data) {
                            //根据数据更新
                            classList.datagrid('reload');
                        },
                        error:function (result) {
                            console.log(result);
                        }
                    });
                });
            });

        }

    });




    xhTool.bindDocKeyDown($('#bt_list_search'));
    $('#bt_list_search').click(function () {
        classList.addDataGridParams(getParams());
    });

    DataGrid.onDblClickRow=function (rowIndex,rowData) {
        if(iUserId!=null){
            var iPCId=rowData['iPCId'];
            window.top.xhTop.showModal({
                title:'编辑公开课',
                content:'<iframe src="'+baseUrl+'Tch/pClassForm?iUserId='+iUserId+'&iPCId='+iPCId+'"  style="width: 100%;;border-style: none;padding: 0px;height:420px"  scrolling="yes"   ></iframe>',
                button:'提交',
                size:'lg',
                backClose:false
            });
        }else {
            var iPCId = rowData['iPCId'];
            var userId = rowData['iUserId'];
            window.top.xhTop.showModal({
                title:'反馈信息',
                content:'<iframe src="'+baseUrl+'Tch/feedback?iPCId='+iPCId+'&iUserId='+userId+'"  style="width: 100%;;border-style: none;padding: 0px;height:350px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose:false
            });
        }
    }

});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}

function getParams() {
    var key = xhExtra.getValue($('#list_search'));
    var startTime   = startDom.getElementValue(),
        endTime     = endDom.getElementValue();
    var type        =xhExtra.getValue($('#select_type'));
    var classType   = xhExtra.getValue($('#select_classType'));
    var depart = xhExtra.getValue($('#list_depart'));
    var params={key:key,startTime:startTime,endTime:endTime,type:type,iUserId:iUserId,classType:classType,depart:depart};
    return params;
}
